#include "expinternalstandardframe.h"
#include "ui_expinternalstandardframe.h"
#include"delegate.h"
#include<QDebug>

ExpInternalStandardFrame::ExpInternalStandardFrame(QWidget *parent,Experiment* pExperiment) :
    QFrame(parent),
    ui(new Ui::ExpInternalStandardFrame)
{
    ui->setupUi(this);
    this->pExperiment=pExperiment;

    ComboxDelegate *pComboxDelegate=new ComboxDelegate(this,1,pExperiment);
    ui->tableWidget_Element->setItemDelegateForColumn(1,pComboxDelegate);
    pComboxDelegate=new ComboxDelegate(this,2,pExperiment);
    ui->tableWidget_InternalS->setItemDelegateForColumn(2,pComboxDelegate);

    LoadData();
}

ExpInternalStandardFrame::~ExpInternalStandardFrame()
{
    delete ui;
}

void ExpInternalStandardFrame::LoadData()
{
    if(pExperiment==NULL) return;

    disconnect(ui->tableWidget_Element,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Element_itemChanged(QTableWidgetItem*)));
    disconnect(ui->tableWidget_InternalS,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_InternalS_itemChanged(QTableWidgetItem*)));

    pExpMethod=pExperiment->GetExpMethodPtr();
    QMap<double,QString> elements=pExperiment->GetQuantitativeElement(1);
    int row=0;
    ui->tableWidget_Element->setRowCount(row);
    foreach(QString name,elements.values())
    {
        EleMethod eleMethod=pExpMethod->GetEleMethod(name);
        InternalPara internalPara=eleMethod.internalPara;
        int colomn=0;

        ui->tableWidget_Element->insertRow(ui->tableWidget_Element->rowCount());
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(name);
        ui->tableWidget_Element->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(internalPara.internalS);
        ui->tableWidget_Element->setItem(row,colomn++,pItem);
        row++;
    }

    elements=pExperiment->GetQuantitativeElement(0);
    row=0;
    ui->tableWidget_InternalS->setRowCount(row);
    foreach(QString name,elements.values())
    {
        EleMethod eleMethod=pExpMethod->GetEleMethod(name);
        InternalPara internalPara=eleMethod.internalPara;
        int colomn=0;

        ui->tableWidget_InternalS->insertRow(ui->tableWidget_InternalS->rowCount());
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(name);
        ui->tableWidget_InternalS->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(internalPara.concentration));
        ui->tableWidget_InternalS->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(internalPara.unit);
        ui->tableWidget_InternalS->setItem(row,colomn++,pItem);
        row++;
    }

    QString text;
    foreach(QString name,elements.values())
    {
        text.append(name+" ");
    }
    ui->comboBox->setCurrentText(QString("[%1]").arg(text));

    connect(ui->tableWidget_Element,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Element_itemChanged(QTableWidgetItem*)));
    connect(ui->tableWidget_InternalS,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_InternalS_itemChanged(QTableWidgetItem*)));
}

void ExpInternalStandardFrame::on_pushButton_Add_clicked()
{
    QModelIndex index=ui->tableWidget_Element->currentIndex();
    if(index.isValid()==false) return;
    QString name=ui->tableWidget_Element->item(index.row(),0)->text();
    EleMethod eleMethod=pExpMethod->GetEleMethod(name);
    eleMethod.internalPara.type=true;
    pExpMethod->SetMethod(name,eleMethod);

    LoadData();
}

void ExpInternalStandardFrame::on_pushButton_Remove_clicked()
{
    QModelIndex index=ui->tableWidget_InternalS->currentIndex();
    if(index.isValid()==false) return;
    QString name=ui->tableWidget_InternalS->item(index.row(),0)->text();
    EleMethod eleMethod=pExpMethod->GetEleMethod(name);
    eleMethod.internalPara.type=false;
    pExpMethod->SetMethod(name,eleMethod);

    LoadData();
}


//changed
void ExpInternalStandardFrame::on_tableWidget_InternalS_itemChanged(QTableWidgetItem *item)
{
    QString name=ui->tableWidget_InternalS->item(item->row(),0)->text();
    EleMethod eleMethod=pExpMethod->GetEleMethod(name);

    QString text=ui->tableWidget_InternalS->item(item->row(),1)->text();
    eleMethod.internalPara.concentration=text.toDouble();
    text=ui->tableWidget_InternalS->item(item->row(),2)->text();
    eleMethod.internalPara.unit=text;
    pExpMethod->SetMethod(name,eleMethod);
}

void ExpInternalStandardFrame::on_tableWidget_Element_itemChanged(QTableWidgetItem *item)
{
    qDebug()<<"item->row()"<<item<<item->row();
    QString name=ui->tableWidget_Element->item(item->row(),0)->text();
    EleMethod eleMethod=pExpMethod->GetEleMethod(name);

    QString text=ui->tableWidget_Element->item(item->row(),1)->text();
    eleMethod.internalPara.internalS=text;
    pExpMethod->SetMethod(name,eleMethod);
}
